package com.baselib.libnetwork.net.model;

import okhttp3.Call;

/**
 * @author lixuce
 * @version v1.0
 * @description
 * @since 2017/10/29
 */

public final class ResultResponse<T> {

    // 响应 body
    private T body;
    private Throwable throwable;
    private okhttp3.Call rawCall;
    private okhttp3.Response rawResponse;

    public static <T> ResultResponse<T> success(T body, Call rawCall, okhttp3.Response rawResponse) {
        ResultResponse<T> response = new ResultResponse<>();
        response.setBody(body);
        response.setRawCall(rawCall);
        response.setRawResponse(rawResponse);
        return response;
    }

    public static <T> ResultResponse<T> error(Call rawCall, Throwable throwable) {
        ResultResponse<T> response = new ResultResponse<>();
        response.setRawCall(rawCall);
        response.setException(throwable);
        return response;
    }

    public ResultResponse() {
    }

    public int code() {
        if (rawResponse == null) {
            return -1;
        }
        return rawResponse.code();
    }

    public String message() {
        if (rawResponse == null) {
            return null;
        }
        return rawResponse.message();
    }


    public boolean isSuccessful() {
        return throwable == null;
    }

    public void setBody(T body) {
        this.body = body;
    }

    public T body() {
        return body;
    }

    public Throwable getException() {
        return throwable;
    }

    public void setException(Throwable exception) {
        this.throwable = exception;
    }

    public Call getRawCall() {
        return rawCall;
    }

    public void setRawCall(Call rawCall) {
        this.rawCall = rawCall;
    }

    public okhttp3.Response getRawResponse() {
        return rawResponse;
    }

    public void setRawResponse(okhttp3.Response rawResponse) {
        this.rawResponse = rawResponse;
    }

}